package com.yunxin.core.utils.infra.utils;

import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;

/**
 * @author zero°
 * @since 2023-06-07
 */
@Slf4j
public enum IdCardNumberUtils {
    /**
     * 实例
     */
    X;

    /**
     * 18位身份证长度
     */
    private static final int LENGTH_18 = 18;
    /**
     * 15位身份证长度
     */
    private static final int LENGTH_15 = 15;

    /**
     * 获取性别
     * @param idCard idCard
     * @return  String 性别
     */
    public String getSexFromIdCard(String idCard) {
        String sex = "未知";
        // 身份证号码为空
        if (CharSequenceUtil.isEmpty(idCard)){
            return "未知";
        }
        if (idCard.length() == LENGTH_18) {
            // 判断性别
            if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {
                sex = "女";
            } else {
                sex = "男";
            }
        } else if (idCard.length() == LENGTH_15) {
            // 用户的性别
            String uSex = idCard.substring(14, 15);
            if (Integer.parseInt(uSex) % 2 == 0) {
                sex = "女";
            } else {
                sex = "男";
            }
        }
        return sex;
    }

    /**
     * 获取性别
     * @param idCard idCard
     * @return  int 性别
     */
    public Integer getSexIntFromIdCard(String idCard) {
        Integer sex = null;
        // 身份证号码为空
        if (CharSequenceUtil.isEmpty(idCard)){
            return null;
        }
        if (idCard.length() == LENGTH_18) {
            // 判断性别
            if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {
                // 女
                sex = 2;
            } else {
                // 男
                sex = 1;
            }
        } else if (idCard.length() == LENGTH_15) {
            // 用户的性别
            String uSex = idCard.substring(14, 15);
            if (Integer.parseInt(uSex) % 2 == 0) {
                // 女
                sex = 2;
            } else {
                // 男
                sex = 1;
            }
        }
        return sex;
    }

    /**
     * 根据身份证的号码算出当前身份证持有者的年龄
     *
     * @param idCard idCard
     * @return  -1(表示异常) 0 (身份证号码为空)
     */
    public int getAgeForIdCard(String idCard) {
        try {
            int age = 0;
            if (CharSequenceUtil.isEmpty(idCard)) {
                return age;
            }
            String birth = "";
            if (idCard.length() == LENGTH_18) {
                birth = idCard.substring(6, 14);
            } else if (idCard.length() == LENGTH_15) {
                birth = "19" + idCard.substring(6, 12);
            }
            int year = Integer.parseInt(birth.substring(0, 4));
            int month = Integer.parseInt(birth.substring(4, 6));
            int day = Integer.parseInt(birth.substring(6));
            Calendar cal = Calendar.getInstance();
            age = cal.get(Calendar.YEAR) - year;
            //周岁计算
            if (cal.get(Calendar.MONTH) < (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
                age--;
            }
            return age;
        } catch (Exception e) {
           log.error("身份证年龄解析异常", e);
        }
        return -1;
    }

}
